/**
 * Created with JetBrains PhpStorm.
 * User: xx
 * Date: 14-1-24
 * Time: 上午11:19
 * To change this template use File | Settings | File Templates.
 * v0.1
 */
(function ($) {
    var xxDialog = {};

    xxDialog.timeOutId = null;

    function randomString(len) {
        len = len || 32;
        var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        var maxPos = $chars.length - 7;
        var pwd = '';
        for (var i = 0; i < len; i++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    }

    /**
     * 默认的关闭窗口函数
     */
    function closeDialog() {
        $("#" + xxDialog.defaults.dialogId).remove();
        if ($.xxDialog.timeOutId) {
            clearTimeout($.xxDialog.timeOutId);
            $.xxDialog.timeOutId = null;
        }
    }

    function createDialogHtml(opts) {
        var html = "<div class=\"m-layer m-layer-show\" id=\"" + opts.dialogId + "\" style=\"display:none;\">" +
            "<div class=\"lymask\"></div><table class=\"lytable\">" +
            "<tbody><tr><td class=\"lytd\">" +
            "<div class=\"lywrap\" style=\"width: " + opts.width + "px;\">" +
            "<div class=\"lytt\"><h2 class=\"u-tt\" id=\"" + opts.titleId + "\">" + opts.title + "</h2><span class=\"lyclose\" id=\"btnClose_" + opts.dialogId + "\">×</span></div>" +
            "<div class=\"lyct\" id=\"" + opts.contentId + "\">" +
            opts.content +
            "</div>" +
            "<div class=\"lybt\">" +
            "<div class=\"lyother\">" +
            "<p id=\"" + opts.lyotherId + "\">" + opts.lyother + "</p>" +
            "</div>" +
            "<div class=\"lybtns\">";
        if (opts.showClose) {
            html += "<button type=\"button\" class=\"u-btn\" id=\"btnFix_" + opts.dialogId + "\">确定</button>";
            //    "<button type=\"button\" class=\"u-btn u-btn-c4\">取消</button>" +
        }
        html += "</div></div></div></td></tr></tbody></table></div>";

        $("body").append(html);

        $("#btnClose_" + opts.dialogId).click(function () {
            opts.funClose();
        });

        $("#btnFix_" + opts.dialogId).click(function () {
            opts.funFix();
        });

        if (opts.timeout > 0) {
            //setTimeout("autoCloseDialog('')", opts.timeout);
            autoCloseDialog(opts.dialogId, opts.timeout);
        }
    }

    function setContent(content) {
        $("#" + xxDialog.defaults.contentId).html(content);
    }

    function displayDialog() {
        if (true || !xxDialog.defaults.dialogId) { //如果ID无值
            xxDialog.defaults.dialogId = randomString(12);
        }

        xxDialog.defaults.titleId = xxDialog.defaults.dialogId + "_title";
        xxDialog.defaults.contentId = xxDialog.defaults.dialogId + "_content";
        xxDialog.defaults.lyotherId = xxDialog.defaults.dialogId + "_lyother";

        if (xxDialog.defaults.type == "ajax") {
            xxDialog.defaults.content = "加载中...";
        }
        createDialogHtml(xxDialog.defaults);
        $("#" + xxDialog.defaults.dialogId).show();

        if (xxDialog.defaults.type == "ajax") {
            $.post(xxDialog.defaults.ajaxUrl, xxDialog.defaults.ajaxParam, function (data) {
                setContent(data);
            })
        }
    }

    xxDialog.show = function (options) {
        xxDialog.defaults = $.extend({}, xxDialog.defaults, options);

        displayDialog();
    }

    // 插件的defaults
    xxDialog.defaults = {
        timeout: 0, //自动关闭时间
        showClose: true, //是否显示关闭按钮
        width: 450,  //对话框宽

        dialogId: "",
        titleId: "",
        contentId: "",
        lyotherId: "",

        lyother: "",
        title: "提示",
        content: "",
        lyother: "",

        type: "content", //content:直接显示文本 ajax:通过url获取文本  frame:子窗口
        ajaxUrl: "",//如果 type 为ajax ,该字段有效
        ajaxParam: {}, //如果 type 为ajax ,该字段有效

        funClose: closeDialog,
        funFix: closeDialog
    };
    $.extend({ xxDialog: xxDialog });
})(jQuery);

function autoCloseDialog(dialogId, timeout) {
    if (timeout > 0) {
        $("#" + dialogId + "_lyother").html((timeout--) + "秒后自动关闭");
        $.xxDialog.timeOutId = setTimeout("autoCloseDialog('" + dialogId + "', " + timeout + ")", 1000);
    } else {
        $("#" + dialogId).remove();
        if ($.xxDialog.timeOutId) {
            clearTimeout($.xxDialog.timeOutId);
            $.xxDialog.timeOutId = null;
        }
    }
}

function xxalert(msg, timeout) {
    var param = {content: msg};
    if (typeof timeout == "number") {
        param = $.extend({}, param, {timeout: timeout});
    }
    $.xxDialog.show(param);
}

function xxwindow(type, ajaxUrl) {
    var param = {ajaxUrl: "/teacher/ajax", type: "ajax"};
    $.xxDialog.show(param);
}


